package com.topdt.jbpm.utils;

/**
 * 
 */

/**
 * @author balance
 * 
 */
public class LogicalUtil {
	/**
	 * @param args
	 */
	public static void main(String[] args) {

//		String exp = "0<=day<=9";
		String exp = "0<day<=3";

		System.out.print(parse(0, "day", exp));
	}

	public static boolean parse(float val, String valName, String exp) {

		String[] exps = exp.split(valName);

		boolean result = true;

		for (String subExp : exps) {
			if(subExp != null && subExp.length() > 0) {
				String sign = subExp.replaceAll("[\\d]", "");
				String numS = subExp.replaceAll("[=<>]", "");
				float num = Integer.valueOf(numS);
				if (subExp.matches("^\\d+[<>=]+")) {
					result = result && logicCalculate(num, val, sign);
				} else {
					result = result && logicCalculate(val, num, sign);
				}
			}
		}
		return result;
	}

	public static boolean logicCalculate(float f, float s, String sign) {
		if (sign.equals("=")) {
			return f == s;
		} else if (sign.equals("<")) {
			return f < s;
		} else if (sign.equals(">")) {
			return f > s;
		} else if (sign.equals("<=")) {
			return f < s || f == s;
		} else if (sign.equals(">=")) {
			return f > s || f == s;
		}
		return false;
	}
}
